package users;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.validator.EmailValidator;

import util.Entity;
import builders.PersonBuilder;
import exceptions.NotAValidMailGivenException;

/**
 * class: User description: represents a phisical Person, not defined role
 * behavior (in Role).
 */

public class Person extends Entity {

    // ---------INSTANCE VARIABLES-----------------------------------------//

    private static final long serialVersionUID = 1L;

    private String name;

    private String surname;

    private UserAccount userAccount;

    private String mail;

    // ---------CONSTRUCTORS------------------------------------------------//

    public Person(final String aName, final String aSurname, final UserAccount anAccount, final String aMail) {
        super();
        name = aName;
        surname = aSurname;
        userAccount = anAccount;
        mail = aMail;
    }

    // ---------ACCESSORS---------------------------------------------------//

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(final String surname) {
        this.surname = surname;
    }

    public UserAccount getUserAccount() {
        return userAccount;
    }

    public void setUserAccount(final UserAccount userAccount) {
        this.userAccount = userAccount;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(final String aMail) {
        if (EmailValidator.getInstance().isValid(aMail)) {
            mail = aMail;
        } else {
            throw new NotAValidMailGivenException("Imput mail is not correct");
        }
    }

    /**
     * Mugre para Hibernate
     */

    public Person() {
        super();
        // Empty constructor
    }

    // ---------OTHER BEHAVIOUR---------------------------------------------//

    public Person(final PersonBuilder builder) {
        super();
        name = builder.getName();
        mail = builder.getMail();
        surname = builder.getSurname();
        userAccount = builder.getUserAccount();

    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 31).append(getName()).append(getSurname()).toHashCode();
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != this.getClass()) {
            return false;
        }

        Person rhs = (Person) obj;
        return new EqualsBuilder().append(getName(), rhs.getName()).append(getSurname(), rhs.getSurname()).isEquals();
    }

}
